package com.ruoyi.web.controller.agriculture;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.agriculture.domain.po.AgLand;
import com.ruoyi.system.agriculture.service.IAgLandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 农业用地Controller
 */
@Tag(name = "农业用地管理")
@RestController
@RequestMapping("/agriculture/land")
public class AgLandController extends BaseController {
    @Autowired
    private IAgLandService landService;

    /**
     * 查询农业用地列表
     */
    @Operation(summary = "获取农业用地列表")
    @Parameters({
        @Parameter(name = "landName", description = "地块名称"),
        @Parameter(name = "soilType", description = "土壤类型"),
        @Parameter(name = "status", description = "地块状态（0空闲 1使用中 2维护中）"),
        @Parameter(name = "pageNum", description = "当前页码"),
        @Parameter(name = "pageSize", description = "每页数量")
    })
    @PreAuthorize("@ss.hasPermi('agriculture:land:list')")
    @GetMapping("/list")
    public TableDataInfo list(AgLand land) {
        startPage();
        LambdaQueryWrapper<AgLand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(land.getLandName() != null, AgLand::getLandName, land.getLandName())
                .eq(land.getSoilType() != null, AgLand::getSoilType, land.getSoilType())
                .eq(land.getStatus() != null, AgLand::getStatus, land.getStatus());
        List<AgLand> list = landService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出农业用地列表
     */
    @Operation(summary = "导出农业用地数据")
    @Parameters({
        @Parameter(name = "landName", description = "地块名称"),
        @Parameter(name = "soilType", description = "土壤类型"),
        @Parameter(name = "status", description = "地块状态")
    })
    @PreAuthorize("@ss.hasPermi('agriculture:land:export')")
    @Log(title = "农业用地", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AgLand land) {
        LambdaQueryWrapper<AgLand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(land.getLandName() != null, AgLand::getLandName, land.getLandName())
                .eq(land.getSoilType() != null, AgLand::getSoilType, land.getSoilType())
                .eq(land.getStatus() != null, AgLand::getStatus, land.getStatus());
        List<AgLand> list = landService.list(queryWrapper);
        ExcelUtil<AgLand> util = new ExcelUtil<>(AgLand.class);
        util.exportExcel(list, "农业用地数据");
    }

    /**
     * 获取农业用地详细信息
     */
    @Operation(summary = "获取农业用地详细信息")
    @Parameter(name = "landId", description = "地块ID", required = true)
    @PreAuthorize("@ss.hasPermi('agriculture:land:query')")
    @GetMapping(value = "/{landId}")
    public AjaxResult getInfo(@PathVariable("landId") Long landId) {
        return success(landService.getById(landId));
    }

    /**
     * 新增农业用地
     */
    @Operation(summary = "新增农业用地")
    @Parameter(name = "land", description = "农业用地信息", required = true)
    @PreAuthorize("@ss.hasPermi('agriculture:land:add')")
    @Log(title = "农业用地", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AgLand land) {
        return toAjax(landService.save(land));
    }

    /**
     * 修改农业用地
     */
    @Operation(summary = "修改农业用地")
    @Parameter(name = "land", description = "农业用地信息", required = true)
    @PreAuthorize("@ss.hasPermi('agriculture:land:edit')")
    @Log(title = "农业用地", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AgLand land) {
        return toAjax(landService.updateById(land));
    }

    /**
     * 删除农业用地
     */
    @Operation(summary = "删除农业用地")
    @Parameter(name = "landIds", description = "地块ID数组", required = true)
    @PreAuthorize("@ss.hasPermi('agriculture:land:remove')")
    @Log(title = "农业用地", businessType = BusinessType.DELETE)
    @DeleteMapping("/{landIds}")
    public AjaxResult remove(@PathVariable Long[] landIds) {
        return toAjax(landService.removeBatchByIds(List.of(landIds)));
    }
} 